package odgs.message.server;

import odgs.Connection;
import odgs.Directory;
import odgs.Result;
import odgs.message.Message;
import odgs.message.MessageHandler;
import odgs.message.MessageProcessor;
import odgs.transaction.DirectoryAddTransaction;

public class DirectoryAddTransactionRequestHandler implements MessageHandler
{
	public void execute(MessageProcessor processor, Message msg, Connection con)
	{
		DirectoryAddTransaction tran = (DirectoryAddTransaction) msg.get("transaction");
		Directory dir = tran.getSource();

		Message reMsg = new Message();
		reMsg.setName(Message.DIRECTORY_ADD_TRANSACTION_RESPONSE);
		reMsg.put("transaction",tran);

		int result = processor.getServerManager().addDirectory(dir);
		if(result > 0)
		{
			reMsg.setResult(Result.DIRECTORY_ADDED_OK);
			processor.getNetworkManager().broadcastMessage(reMsg);
		}
		else
		{
			reMsg.setResult(result);
			con.sendMessage(reMsg);
		}
	}
}